package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.controller.param.MessageParam;
import com.bestcem.xm.om.controller.param.NoticeInfoParam;
import com.bestcem.xm.om.dao.SmsRecordDao;
import com.bestcem.xm.om.dao.mapper.SmsRecordMapper;
import com.bestcem.xm.om.entity.SmsRecord;
import com.bestcem.xm.om.enums.SendStatusEnum;
import com.bestcem.xm.om.service.dto.common.PageDTO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 描述信息
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/11/16 17:10
 */
@Repository
public class SmsRecordDaoImpl implements SmsRecordDao {
    @Resource
    private SmsRecordMapper smsRecordMapper;

    @Override
    public List<SmsRecord> listByBatchId(String batchId) {
        return smsRecordMapper.listByBatchId(batchId);
    }

    @Override
    public List<SmsRecord> listByIds(List<String> idList) {
        return smsRecordMapper.listByIds(idList);

    }

    @Override
    public Long countByCondition(NoticeInfoParam noticeInfoParam) {
        return smsRecordMapper.countByCondition(noticeInfoParam);
    }

    @Override
    public List<SmsRecord> listByCondition(NoticeInfoParam noticeInfoParam) {
        if (ObjectUtils.allNotNull(noticeInfoParam.getPage(), noticeInfoParam.getSize())) {
            noticeInfoParam.setOffset((noticeInfoParam.getPage() - 1) * noticeInfoParam.getSize());
        }
        return smsRecordMapper.listByCondition(noticeInfoParam);
    }

    @Override
    public Integer saveBatch(List<SmsRecord> smsRecordList) {
        smsRecordList.forEach(smsRecord -> smsRecord.setId(ObjectId.next()));
        return smsRecordMapper.saveBatch(smsRecordList);
    }

    @Override
    public void update(SmsRecord smsRecord) {
        if (ObjectId.isValid(smsRecord.getId())) {
            smsRecordMapper.update(smsRecord);
        }
    }

    @Override
    public Long removeRecordByBashId(String batchId) {
        if (!ObjectId.isValid(batchId)) {
            return 0L;
        }
        return smsRecordMapper.removeRecordByBashId(batchId);

    }

    @Override
    public PageDTO<SmsRecord> listByMessageParam(MessageParam param, List<String> ids) {
        PageDTO<SmsRecord> pageDTO = new PageDTO<>();
        pageDTO.setTotal(smsRecordMapper.countByMessageParam(param, ids));
        if (Objects.nonNull(param.getPage()) && Objects.nonNull(param.getSize())) {
            param.setOffset((param.getPage() - 1) * param.getSize());
        }
        pageDTO.setData(smsRecordMapper.listByMessageParam(param, ids));
        return pageDTO;
    }

    @Override
    public SmsRecord findById(String id) {
        if (ObjectId.isValid(id)) {
            return smsRecordMapper.findById(id);
        }
        return null;
    }

    @Override
    public void updateSendStatus(String batchId, List<String> idList, SendStatusEnum sendStatusEnum) {
        smsRecordMapper.updateSendStatus(batchId, idList, sendStatusEnum.getIndex(), LocalDateTime.now());
    }

    @Override
    public List<SmsRecord> listByBatchIdAndStatus(String batchId, SendStatusEnum sendStatusEnum) {
        return smsRecordMapper.listByBatchIdAndStatus(batchId, sendStatusEnum.getIndex());
    }

    @Override
    public List<SmsRecord> listByVistedTime(LocalDateTime from) {
        return smsRecordMapper.listByVistedTime(from);
    }

    @Override
    public long countByStatus(String batchId, SendStatusEnum sendStatusEnum) {
        if (ObjectId.isValid(batchId)) {
            return smsRecordMapper.countByStatus(batchId, sendStatusEnum.getIndex());
        }
        return 0L;
    }

    @Override
    public void insert(SmsRecord smsRecord) {
        smsRecord.setId(ObjectId.next());
        smsRecordMapper.insert(smsRecord);
    }

    @Override
    public Long countLinkVisited(String batchId) {
        if (ObjectId.isValid(batchId)) {
            return smsRecordMapper.countLinkVisited(batchId);
        }
        return 0L;
    }

    @Override
    public Long countSuccess(String batchId) {
        if (ObjectId.isValid(batchId)) {
            return smsRecordMapper.countSuccess(batchId);
        }
        return 0L;
    }
}
